package cn.edu.shou.lab.config.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RefreshFileUtil {
    public static List<File> refresh(String searchDirPath, String ext){
        List<File> results = new ArrayList<>();

        if(searchDirPath == null || searchDirPath.isEmpty()){
            throw new RuntimeException("search file directory can not be null !");
        }
        if(ext == null){
            throw new RuntimeException("search file ext can not be null !");
        }else{
            if(!ext.isEmpty() && ext.indexOf(".") != 0){
                ext = "." + ext;
            }
        }
        File searchDir = new File(searchDirPath);
        if(!searchDir.isDirectory()){
            throw new RuntimeException("search file directory is not available !");
        }
        File[] files = searchDir.listFiles();
        if(files != null) {
            for(File file : files){
                String filename = file.getName();
                String fileext = filename.substring(filename.indexOf("."), filename.length());
                if(fileext.equalsIgnoreCase(ext)){
                    results.add(file);
                }
            }
        }
        return results;
    }

}
